<template>
    <div class="main commodity-tag-list">
        <div class="filter-container">
            <el-input @keyup.enter.native="search" class="filter-item" placeholder="按商品名称搜索" v-model="listQuery.goodsName" clearable></el-input>
            <el-input @keyup.enter.native="search" class="filter-item" placeholder="按sku编码搜索" v-model="listQuery.skuCode" clearable></el-input>
            <el-input @keyup.enter.native="search" class="filter-item" placeholder="按商品编码搜索" v-model="listQuery.serialNo" clearable></el-input>
            <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
            <!--<el-button class="filter-item" v-if="organizationId===1&&skuType===1" type="danger" v-loading.fullscreen.lock="syncLoading" element-loading-background="rgba(0, 0, 0, 0.7)" element-loading-text="SKU同步中" @click="syncAllSKU">同步</el-button>-->
            <span v-if="realTimeSalesVolume.serialNo && realTimeSalesVolume.soldNumber>=0">【商品编码：{{realTimeSalesVolume.serialNo}}】已售：{{realTimeSalesVolume.soldNumber}}</span>
            <el-button type="primary" class="fr" @click="goSetSku" v-if="rightsButtons['set']">库存告警设置</el-button>
        </div>
        <el-table :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border highlight-current-row>
            <el-table-column align="center" label="商品名称" prop="goodsName"></el-table-column>
            <el-table-column align="center" label="商品编码" prop="serialNo"></el-table-column>
            <el-table-column align="center" label="属性值1" prop="attrValue"></el-table-column>
            <el-table-column align="center" label="属性值2" prop="attrValue2"></el-table-column>
            <el-table-column align="center" label="SKU数量" prop="skuNumber" width="100"></el-table-column>
            <el-table-column align="center" label="可分配SKU数量" prop="distributableSkuNumber" width="120"></el-table-column>
            <el-table-column align="center" label="已占用SKU数量" prop="usedSkuNumber" width="120"></el-table-column>
            <el-table-column align="center" label="SKU编码" prop="skuCode"></el-table-column>
            <el-table-column align="center" label="同步时间" v-if="organizationId===1&&skuType===1" width="100">
                <template slot-scope="scope">
                    {{formatDate(scope.row.updatedAt, 'YYYY-MM-DD HH:mm:ss')}}
                </template>
            </el-table-column>
            <el-table-column align="center" label="是否可用" width="80">
                <template slot-scope="scope">
                    {{scope.row.deleteFlag?'是':'否'}}
                </template>
            </el-table-column>
            <!--<el-table-column align="center" label="操作" width="100">
                <template slot-scope="scope">
                    <el-button v-if="organizationId===1&&skuType===1" type="danger" size="mini"@click="syncSignalSKU(scope.row.skuCode)">同步</el-button>
                    <el-button v-else type="primary" size="mini" plain @click="setSkuNumber(scope.row)">设置库存</el-button>
                </template>
            </el-table-column>-->
        </el-table>
        <div class="pagination-container" v-if="listTotal > 0">
            <el-pagination @current-change="handleCurrentChange" :current-page.sync="listQuery.pageIndex" :page-size="listQuery.pageSize" :total="listTotal" layout="total, prev, pager, next, jumper"></el-pagination>
        </div>
        <el-dialog title="设置库存" :visible.sync="showEditPop">
            <el-form :model="formData" :rules="formRules" size="medium" ref="form" label-width="120px">
                <el-form-item label="商品编码:" prop="serialNo">
                    <span>{{formData.serialNo}}</span>
                </el-form-item>
                <el-form-item label="属性值1:" prop="attrValue">
                    <span>{{formData.attrValue}}</span>
                </el-form-item>
                <el-form-item label="属性值2:" prop="attrValue2">
                    <span>{{formData.attrValue2}}</span>
                </el-form-item>
                <el-form-item label="SKU编码:" prop="skuCode">
                    <span>{{formData.skuCode}}</span>
                </el-form-item>
                <el-form-item label="SKU数量:" prop="skuNumber">
                    <el-input v-model="formData.skuNumber" maxLength="15"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="save" :loading="btnLoading">保存</el-button>
                <el-button @click="showEditPop = false">取消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
  import { regexp } from '../../../utils/tools'
  export default {
    name: 'commoditySku',
    props: {
      skuType: {default: 1}
    },
    data() {
      return {
        syncLoading: false,
        list: [],
        organizationId: this.$store.getters.organizationId,
        listQuery: {
          goodsName: '',
          skuCode: '',
          serialNo: '',
          pageIndex: 1,
          pageSize: 15,
          organizationId: this.$store.getters.organizationId,
          goodsType: 1
        },
        listTotal: 0,
        listLoading: false,
        realTimeSalesVolume: {
          serialNo: '',
          soldNumber: 0
        },

        showEditPop: false,
        btnLoading: false,
        formData: {
        },
        formRules: {
          skuNumber: [
            {required: true, message: '不能为空', trigger: 'change'},
            {pattern: regexp.positiveInt, message: '格式错误：必须为正整数', trigger: 'change'}
          ]
        }
      }
    },
    watch: {
      $route () {
        this.getList();
      }
    },
    created() {
      this.getList();
    },
    methods: {
      // 获取列表
      getList() {
        this.listLoading = true;
        this.listQuery.goodsType = this.organizationId!==1?2:this.skuType;
        /*if(!(this.organizationId===1&&this.skuType===1)){
            this.listQuery.organizationId = this.organizationId
        }
        else{
            this.listQuery.organizationId = ''
        }*/
        // let url = this.organizationId===1&&this.skuType===1?'/bsnl-product/goodssku/query':'/bsnl-product/goodsSkuVirtual/query'
        this.request({
          url: '/bsnl-product/goodssku/query',
          method: 'post',
          data: this.listQuery
        }).then(({data, totalCount}) => {
          this.list = data;
          this.listTotal = totalCount;
          this.listLoading = false;
        }).catch(err => {
          console.log(err);
          this.list = [];
          this.listTotal = 0;
          this.listLoading = false;
        });
      },
      // 搜索
      search() {
        this.listQuery.pageIndex = 1;
        this.getList();
        // this.searchInventory();
      },
      // 搜索
      searchInventory() {
        if(this.listQuery.serialNo){
          this.request({
            url: `/bsnl-product/goodssku/searchInventory?serialNo=${this.listQuery.serialNo}`,
            method: 'get'
          }).then(({data}) => {
            this.realTimeSalesVolume.serialNo = this.listQuery.serialNo;
            this.realTimeSalesVolume.soldNumber = data.number
          }).catch(err => {
            console.log(err);
            this.realTimeSalesVolume.serialNo = '';
            this.realTimeSalesVolume.soldNumber = 0
          });
        } else {
          this.realTimeSalesVolume.serialNo = '';
          this.realTimeSalesVolume.soldNumber = 0
        }
      },
      // 切换分页
      handleCurrentChange(page) {
        this.listQuery.pageIndex = page;
        this.getList();
      },
      // 同步本页SKU
      syncAllSKU() {
        let kc_data = this.list.map(item => {
          return {
            sku: item.skuCode,
            sddm: 'bsnlApp'
          }
        });
        this.syncSKU(kc_data);
      },
      // 同步单个SKU
      syncSignalSKU(skuCode) {
        let kc_data = [{
          sku: skuCode,
          sddm: 'bsnlApp'
        }];
        this.syncSKU(kc_data);
      },
      // 同步SKU
      syncSKU(kucunList) {
        this.$confirm('确认进行SKU同步?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.syncLoading = true;
          this.request({
            url: `/bsnl-product/erp/bsnl/updateGetKucun`,
            method: 'post',
            data: {
              kucunList
            }
          }).then((response) => {
            console.log(response);
            this.syncLoading = false;
            this.$message.success('SKU同步成功')
            this.getList();
          }).catch(err => {
            console.log(err);
            this.syncLoading = false;
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消SKU同步'
          });
        });
      },

      // 显示设置SKU NUMBER
      setSkuNumber(row) {
        this.formData = Object.assign({}, row);
        this.showEditPop = true;
      },
      // 设置SKU NUMBER
      save() {
        this.$refs.form.validate(valid => {
          if (!valid) {
            return false;
          }
          this.btnLoading = true;
          this.request({
            url: '/bsnl-product/goodsSkuVirtual/update',
            method: 'post',
            data: this.formData
          }).then(() => {
            this.btnLoading = false;
            this.showEditPop = false;
            this.$message.success('设置成功');
            this.getList();
          }).catch(err => {
            this.btnLoading = false;
          });
        });
      },
      goSetSku() {
        this.$router.push({
          path: '/commodity/skuWarn'
        })
      }
    }
  }
</script>
